package cn.com.homeyoda.aimate.aihealth.ui;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.wzq.mvvmsmart.base.BaseModelMVVM;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.FaceInfoBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainViewModel extends BaseViewModel<Repository> {

    public StateLiveData<FaceInfoBean.DataBean> faceInfoData;


    public MainViewModel(@NonNull Application application, BaseModelMVVM model) {
        super(application, model);
        faceInfoData = new StateLiveData<>();
    }

    public void queryFaceInfo() {
        Log.i("huadi","model.getFaceId()===="+model.getFaceId());
        Log.i("huadi","model.getFamilyId()===="+model.getFamilyId());
        model.queryFaceInfo(model.getFaceId(), model.getFamilyId())
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MainViewModel.this)
                .subscribe(new Observer<FaceInfoBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        faceInfoData.postLoading();
                    }

                    @Override
                    public void onNext(FaceInfoBean faceInfoBean) {
                        faceInfoData.postValueAndSuccess(faceInfoBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        faceInfoData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }
}
